package com.rupertjones.globalcron.web;

import com.rupertjones.globalcron.domain.User;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.controller.LifecycleStage;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>&copy Rupert Jones 2011,2012</p>
 *
 * @author rup
 */
public class InfoUsersAction extends AbstractAction {

    @Before(stages = LifecycleStage.BindingAndValidation)
    public void loadAndBind() {

    }

    @DefaultHandler
    public Resolution view() {
        return new ForwardResolution("/WEB-INF/jsp/info_users.jsp");
    }

    public List<FormUser> getUsers() {
         return listFormUsers();
    }

    private List<FormUser> listFormUsers() {
        List<FormUser> formUsers = new ArrayList<FormUser>();

        for(User user : getUserService().findAllUsers()) {
            FormUser formUser = new FormUser();
            formUser.setId(user.getId());
            formUser.setFullName(user.getFullName());
            formUser.setUsername(user.getUsername());
            formUser.setRoles(user.getRoles());
            formUser.setLastLogin(user.getLastLogin());
            formUsers.add(formUser);
        }
        return formUsers;
    }
}
